//
//  CinemaViewController.m
//  DouBan
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 zuzhiming. All rights reserved.
//

#import "CinemaViewController.h"
#import "CollectionViewCell.h"
#import "CinemaModel.h"
@interface CinemaViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionView *cinemaCollectionView;

@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation CinemaViewController

static NSString *const collectionViewCell = @"collectionViewCell";

- (void)dealloc
{
    [_cinemaCollectionView release];
    [_dataArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self dataHandle];
    
    [self createCinemaCollectionView];
}

- (void)dataHandle
{
    self.dataArr = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cinemalist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSDictionary *dict = [dictionary objectForKey:@"result"];
    NSArray *dataArray = [dict objectForKey:@"data"];
    for (NSDictionary *dic in dataArray) {
        CinemaModel *model = [[CinemaModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataArr addObject:model];
        [model release];
    }
    
}

- (void)createCinemaCollectionView
{
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.itemSize = CGSizeMake(SCREEN_WIDTH, (SCREEN_HEIGHT - 64 - 44 - 30) / 4);
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowlayout.minimumLineSpacing = 0;
    flowlayout.minimumInteritemSpacing = 0;
    
    self.cinemaCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 44) collectionViewLayout:flowlayout];
    self.cinemaCollectionView.backgroundColor = [UIColor whiteColor];
    self.cinemaCollectionView.delegate = self;
    self.cinemaCollectionView.dataSource = self;
    
    [flowlayout release];
    [self.view addSubview:_cinemaCollectionView];
    [_cinemaCollectionView release];
    
    [self.cinemaCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:collectionViewCell];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCell forIndexPath:indexPath];
    CinemaModel *cinemaModel = [self.dataArr objectAtIndex:indexPath.row];
    cell.model = cinemaModel;
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
